﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages.Profile
{
    public class TreatmentAjax : BasePage
    {
        private int showMode = -1;

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            if (IsPost == false) HandleGet();
        }

        protected void HandleGet()
        {
            int id = RequestTools.GetQueryInt("id", 0);
            if (id <= 0) return;

            showMode = RequestTools.GetQueryInt("showMode", Constants.DEFAULTMODE);
            PatientRecordInfo prData = PatientBLL.Instance.GetPatientRecordInfo(id);
            if (prData.Id <= 0)
                return;

            MakeGraphData(prData);
        }

        private void MakeGraphData(PatientRecordInfo prData)
        {
            int lastId = -1, lastTypeId = -1, groupIdx = 0;
            TreatmentHistoryInfo item = null;
            string tmpTimeStr = "", tmpValueStr = "", tmpPurposeStr = "";
            int curTypeCount = 0;

            List<int> groupList = new List<int>() ;
            StringBuilder sb = new StringBuilder();

            sb.Append("&Treatment=1&\r\n");
            sb.AppendFormat("&ShowMode={0}&\r\n", showMode);
            sb.AppendFormat("&StartDate={0:yyyy-MM-dd}&\r\n", prData.StartDate);
            sb.AppendFormat("&DosageDate={0:yyyy-MM-dd}&\r\n", prData.ConfirmDate);
            sb.AppendFormat("&UpdateDate={0:yyyy-MM-dd}&\r\n", prData.UpdateDate);

            List<TreatmentHistoryInfo> lst = PatientTreatmentBLL.Instance.GetTreatementHistoryList(prData.Id, Constants.ALLMODE);
            for (int i = 0; i < lst.Count; i++)
            {
                item = lst[i];

                //目前只需要考虑一组的显示
                ////一个新的类型结束
                //if (item.TreatmentTypeId != lastTypeId && lastTypeId != -1)
                //{
                //    sb.AppendFormat("&Group{0}={1};{2}&\r\n", groupIdx + 1, TreatmentBLL.Instance.GetTreatmentInfo(item.TreatmentTypeId).TypeName,
                //        curTypeCount);
                //    groupIdx++;
                //    curTypeCount = 0;   //重新归零
                //}

                //如果是一个新的治疗开始，则需要回写治疗数据
                if (item.TreatmentId != lastId && i != 0)
                {
                    tmpTimeStr += "&\r\n";
                    tmpValueStr += "&\r\n";
                    tmpPurposeStr += "&\r\n";
                    sb.AppendFormat(tmpTimeStr);
                    sb.AppendFormat(tmpValueStr);
                    sb.AppendFormat(tmpPurposeStr);
                }

                if (item.TreatmentId != lastId )
                {
                    curTypeCount++;
                    sb.AppendFormat("&Var_1_{0}={1};{2}/{3}&\r\n", curTypeCount, item.TreatmentName, item.Unit, item.Frequency);
                    tmpTimeStr = string.Format("&Time_1_{0}={1}", curTypeCount, item.StartDate.ToString("yyyy-MM-dd"));
                    tmpValueStr = string.Format("&Value_1_{0}={1}", curTypeCount, item.DataValue);
                    tmpPurposeStr = string.Format("&Purpose_1_{0}={1}", curTypeCount, item.State != Constants.STOPED_USE ? item.UsePurpose : -1);
                }
                else //if(item.TreatmentId == lastId)
                {
                    tmpTimeStr += string.Format(";{0}", item.StartDate.ToString("yyyy-MM-dd"));
                    tmpValueStr += string.Format(";{0}", item.DataValue);
                    tmpPurposeStr += string.Format(";{0}", item.State != Constants.STOPED_USE ? item.UsePurpose : -1);
                }

                lastId = item.TreatmentId;
                lastTypeId = item.TreatmentTypeId;
            }

            //对于最后一个
            groupIdx++;
            if (lst.Count > 0)
            {
                tmpTimeStr += "&\r\n";
                tmpValueStr += "&\r\n";
                tmpPurposeStr += "&\r\n";
                sb.AppendFormat(tmpTimeStr);
                sb.AppendFormat(tmpValueStr);
                sb.AppendFormat(tmpPurposeStr);

                sb.AppendFormat("&Group{0}={1};{2}&\r\n", groupIdx, "使用目的"/*TreatmentBLL.Instance.GetTreatmentTypeInfo(lastTypeId).Name*/,
                    curTypeCount);
            }
            else //if(lst.Count == 0)
            {
                sb.AppendFormat("&Group1=药物治疗;0&\r\n");
            }

            //输出关于组信息
            sb.AppendFormat("\r\n&GroupCount={0}&\r\n", groupIdx);

            ResponseXML(sb);
        }

        private void ResponseXML(StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
    }
}